package com.example.home.park;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.home.R;
import com.example.home.adapter.CatcomitAdapter;
import com.example.home.bean.CatBean;
import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class CatActivity extends AppCompatActivity {

    private RecyclerView rcv_cat;
    private OkHttpClient okHttpClient;
    private Handler handler;
    private CatBean catBean;
    private Button more;
    private CatcomitAdapter catcomitAdapter;
    private ImageView back;
    private TextView record;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cat);
        more = findViewById(R.id.more);

        initview();
        getCatcomit();
    }

    private void getCatcomit() {
        Request request = new Request.Builder().url("http://124.93.196.45:10001/prod-api/api/park/lot/list").build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                e.printStackTrace();
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                String json = response.body().string();
                Log.e("tag","访问成功");
                Gson gson = new Gson();
                catBean = gson.fromJson(json, CatBean.class);
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        bindnewData();
                    }
                });
            }
        });
    }

    private void bindnewData() {
        catcomitAdapter = new CatcomitAdapter(this, catBean.getRows());
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        rcv_cat.setLayoutManager(linearLayoutManager);
        rcv_cat.setAdapter(catcomitAdapter);
    }

    private void initview() {
        rcv_cat = findViewById(R.id.rcv_cat);
        okHttpClient = new OkHttpClient();
        back = findViewById(R.id.back);
        record = findViewById(R.id.record);
        handler = new Handler();
        // 添加 "更多" 按钮的点击事件监听器
        more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 当用户点击 "更多" 按钮时，显示所有数据
                if (catcomitAdapter != null) {
                    catcomitAdapter.setShowAllData(true);
                }
            }
        });
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        //进入列表页面
        record.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(CatActivity.this,RecordActivity.class);
                startActivity(intent);
            }
        });

    }

}
